package id.amikom.jogjabankfinder.activity;

import java.util.ArrayList;

import org.json.JSONObject;

import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;
import id.amikom.jogjabankfinder.JSONHelper;
import id.amikom.jogjabankfinder.R;
import id.amikom.jogjabankfinder.entity.Kurs;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class Konversi extends RoboActivity implements OnClickListener, OnItemSelectedListener
{
	@InjectView(R.id.etKonversiNominal)
	EditText					etNominal;
	@InjectView(R.id.etKonversiHasil)
	EditText					etHasil;
	@InjectView(R.id.spinnerKonversi)
	Spinner						spKonversi;
	@InjectView(R.id.textKurs)
	TextView					txtKurs;
	@InjectView(R.id.btnKonversiHitung)
	Button						btnKonversi;

	private JSONHelper			jsonHelper;
	private ArrayList<Kurs>		listKurs;
	private ArrayList<String>	kodeKurs;

	ProgressDialog				pDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_konversi);

		spKonversi.setOnItemSelectedListener(this);
		btnKonversi.setOnClickListener(this);
		jsonHelper = new JSONHelper();

		new AsycnTaskKurs().execute();

	}

	private void aturText()
	{
		txtKurs.setText("" + getNominal());
		etNominal.setText("1");
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			startActivity(new Intent(Konversi.this, MenuUtama.class));
			finish();
		}
		return super.onKeyDown(keyCode, event);

	}

	private double getNominal()
	{
		int i = spKonversi.getSelectedItemPosition();
		return listKurs.get(i).getNominal();
	}

	@Override
	public void onClick(View v)
	{
		if (v == btnKonversi)
		{
			double nominal = Double.parseDouble(etNominal.getText().toString());
			double hasil = nominal * getNominal();

			etHasil.setText("" + Double.valueOf(hasil).longValue());
		}

	}

	private void tampilKeSpinner()
	{
		// TODO Auto-generated method stub
		ArrayAdapter<String> kodeAdapter = new ArrayAdapter<String>(Konversi.this,
				android.R.layout.simple_spinner_item, kodeKurs);
		kodeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spKonversi.setAdapter(kodeAdapter);
	}

	private class AsycnTaskKurs extends AsyncTask<Void, Void, Void>
	{

		@Override
		protected void onPreExecute()
		{
			// TODO Auto-generated method stub
			super.onPreExecute();

			pDialog = new ProgressDialog(Konversi.this);
			pDialog.setMessage("Loading....");
			pDialog.setCancelable(true);
			pDialog.show();
		}

		@Override
		protected void onPostExecute(Void result)
		{
			// TODO Auto-generated method stub

			pDialog.dismiss();

			runOnUiThread(new Runnable()
			{

				@Override
				public void run()
				{

					kodeKurs = new ArrayList<String>();

					for (int i = 0; i < listKurs.size(); i++)
					{
						kodeKurs.add(listKurs.get(i).getKode());
					}
				}

			});
			tampilKeSpinner();
			aturText();
			super.onPostExecute(result);
		}

		@Override
		protected Void doInBackground(Void... params)
		{
			// TODO Auto-generated method stub
			String url = "http://jogjabankfinder.tk/kursmatauang.php";

			JSONObject jsonObject = jsonHelper.getJSONFromUrl(url);
			listKurs = jsonHelper.getAllKurs(jsonObject);
			return null;
		}

	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int loc, long arg3)
	{
		txtKurs.setText("" + listKurs.get(loc).getNominal());
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0)
	{
		// TODO Auto-generated method stub

	}
}